﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTemplate.DAL.Repositories;
using WebTemplate.DAL.Entities;

namespace WebTemplate.ControlPanel
{
    public partial class NewsDetails : System.Web.UI.Page
    {
        private int? Id
        {
            get
            {
                var idstr = this.Request.QueryString["id"];
                if (string.IsNullOrWhiteSpace(idstr))
                    return null;
                return int.Parse(idstr);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Id.HasValue)
                {
                    NewsRepository repository = new NewsRepository();
                    var news = repository.Select(new NewsSearchCriteria() { Id = this.Id.Value }).First();

                    this.txtTitle.Text = news.Title;
                    this.txtShortDescription.Text = news.ShortDescription;
                    this.txtContent.Text = news.Description;
                    this.txtDate.Text = news.Date.ToString("yyyy-MM-dd");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                string fullVirtualPath = null;
                if (txtImage.HasFiles)
                {
                    string filename = Guid.NewGuid().ToString();
                    string fileExtention = Path.GetExtension(txtImage.FileName);
                    fullVirtualPath = "~/Uploading/" + filename + fileExtention;
                    txtImage.SaveAs(Server.MapPath(fullVirtualPath));
                }


                NewsRepository repository = new NewsRepository();
                News news;
                if (Id.HasValue)
                {
                    news = repository.Select(new NewsSearchCriteria() { Id = this.Id.Value }).First();
                }
                else
                {
                    news = new News();
                }

                news.Title = txtTitle.Text;
                news.ShortDescription = txtShortDescription.Text;
                news.Description = txtContent.Text;
                news.ImageUrl = fullVirtualPath ?? news.ImageUrl;
                news.Date = Convert.ToDateTime(txtDate.Text);

                if (Id.HasValue)
                {
                    news.UpdateDate = DateTime.Now;
                    news.UpdateBy = 0;
                    repository.Update(news);
                }
                else
                {
                    news.CreationDate = DateTime.Now;
                    news.CreatedBy = 0;
                    repository.Insert(news);
                }

                Response.Redirect("~/news/NewsList.aspx");
            }

            //NewsRepository repository = new NewsRepository();
            //repository.Insert('" + txtTitle + "','" + txtShortDescription.Text + "',"' + txtContent.Text +"')",  );
        }


    }
}